package com.example.dragcontentviewdemo.activity.ui.drawable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class TranslucentSolidDrawable extends Drawable{
    public static final int CONSTANT_MAX_COLOR_VALUE = 255;

    private int alpha;
    private int r = 0;
    private int g = 0;
    private int b = 0;

    public TranslucentSolidDrawable(int alpha, int r, int g, int b){
        this.alpha = limitRange(alpha);
        this.r = limitRange(r);
        this.g = limitRange(g);
        this.b = limitRange(b);
    }

    /**
     * rgb均为0，便携构造函数
     * @param alpha
     */
    public TranslucentSolidDrawable(int alpha){
        this.alpha = limitRange(alpha);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.drawARGB(alpha, r, g, b);
    }

    @Override
    public void setAlpha(int alpha) {
        this.alpha = limitRange(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {}

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getAlpha() {
        return alpha;
    }

    private int limitRange(int value){
        return Math.max(0, Math.min(CONSTANT_MAX_COLOR_VALUE, value));
    }
}
